{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Open Ended GPT2 Text Generation Explanations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook demonstrates how to get explanations for the output of gpt2 used for open ended text generation. In this demo, we use the pretrained gpt2 model provided by hugging face (https://huggingface.co/gpt2) to explain the generated text by gpt2. We further showcase how to get explanations for custom output generated text and plot global input token importances for any output generated token." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "\n", "import shap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load model and tokenizer" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "tokenizer = AutoTokenizer.from_pretrained(\"gpt2\", use_fast=True)\n", "model = AutoModelForCausalLM.from_pretrained(\"gpt2\").cuda()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Below, we set certain model configurations. We need to define if the model is a decoder or encoder-decoder.\n", "This can be set through the 'is_decoder' or 'is_encoder_decoder' param in model's config file.\n", "We can also set custom model generation parameters which will be used during the output text generation decoding process." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# set model decoder to true\n", "model.config.is_decoder = True\n", "# set text-generation params under task_specific_params\n", "model.config.task_specific_params[\"text-generation\"] = {\n", " \"do_sample\": True,\n", " \"max_length\": 50,\n", " \"temperature\": 0.7,\n", " \"top_k\": 50,\n", " \"no_repeat_ngram_size\": 2,\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Define initial text" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "s = [\"I enjoy walking with my cute dog\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an explainer object and compute the SHAP values" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n" ] } ], "source": [ "explainer = shap.Explainer(model, tokenizer)\n", "shap_values = explainer(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Visualize shap explanations" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
[0]
\n", "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n", "
outputs
\n", "
,
\n", "
but
\n", "
I
\n", "
'm
\n", "
not
\n", "
sure
\n", "
if
\n", "
I
\n", "
'll
\n", "
ever
\n", "
be
\n", "
able
\n", "
to


0-2-424-4.04941-4.04941base value-1.27522-1.27522f,(inputs)4.064 dog 0.072 with -0.431 enjoy -0.427 walking -0.238 cute -0.15 my -0.117 I
inputs
-0.117
I
-0.431
enjoy
-0.427
walking
0.072
with
-0.15
my
-0.238
cute
4.064
dog